package GUI.Cliente;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableItem;

import client.entities.ClientVO;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.client.interfaces.ClientProxyMgt;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class VerClientesLista extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public VerClientesLista(Shell parent, int style) {
		super(parent, style);
		setText("Listado de Clientes");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(557, 363);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.left = new FormAttachment(0, 56);
		fd_table.right = new FormAttachment(100, -56);
		fd_table.top = new FormAttachment(0, 38);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnCedula = new TableColumn(table, SWT.NONE);
		tblclmnCedula.setWidth(116);
		tblclmnCedula.setText("Cedula");
		
		TableColumn tblclmnNombre = new TableColumn(table, SWT.NONE);
		tblclmnNombre.setWidth(167);
		tblclmnNombre.setText("Nombre");
		
		TableColumn tblclmnApellido = new TableColumn(table, SWT.NONE);
		tblclmnApellido.setWidth(158);
		tblclmnApellido.setText("Apellido");
		
		Button btnVerMasDetalles = new Button(shell, SWT.NONE);
		btnVerMasDetalles.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table.getSelection()[0];
				if(t==null)
				{
					}
				else{
					ClientProxyMgt uso = ServiceFacade.getInstance().getClientProxyMgt();
					ClientVO cliente=null;
					ClientVO[] clients=null;
					try {
						clients = uso.getClients();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					String c = t.getText(0);
					for(int i = 0; i<clients.length;i++){
						if(c.equalsIgnoreCase(String.valueOf(clients[i].getClientId()))){
							cliente=clients[i];
							
							
						}
					}
				DatosCliente d = new DatosCliente(shell, shell.getStyle(),cliente,1);
				shell.setVisible(false);
				d.open();
				
				
				}
				}
		});
		fd_table.bottom = new FormAttachment(btnVerMasDetalles, -19);
		FormData fd_btnVerMasDetalles = new FormData();
		fd_btnVerMasDetalles.top = new FormAttachment(0, 278);
		fd_btnVerMasDetalles.right = new FormAttachment(100, -102);
		btnVerMasDetalles.setLayoutData(fd_btnVerMasDetalles);
		btnVerMasDetalles.setText("Ver Mas Detalles");
		
		
		
		ClientProxyMgt uso = ServiceFacade.getInstance().getClientProxyMgt();
		ClientVO [] clients = uso.getClients();
		
		for(int i=0; i<clients.length;i++){
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(0, String.valueOf(clients[i].getClientId()));
			tableItem.setText(1, clients[i].getName());
			tableItem.setText(2, clients[i].getLastName());
			
		}
		

	}
}
